package com.bosch.in.rtp1.ept.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bosch.in.rtp1.ept.constant.ModifiedLinksContanst;
import com.bosch.in.rtp1.ept.dao.LinkListDao;
import com.bosch.in.rtp1.ept.model.dto.Link;
import com.bosch.in.rtp1.ept.util.SQLStatementLoader;

public class LinkListJDBCDao extends GenericDao implements LinkListDao{

	/**
	 * Constructor to inject connection
	 * @param con	DB connection
	 */
	
	public LinkListJDBCDao(Connection connection) {
		super(connection);
	}
	
	@Override
	public List<Link> getLinkList() throws SQLException {
		String queryString = SQLStatementLoader.getLinkListQuery();
		//excute query
		ResultSet resultSet = executeQuery(queryString,null);
		//convert result set 
		List<Link> links = new ArrayList<Link>();
		int order = 1;
		while (resultSet.next()) {
			// save result set to LotStatus object
			Link link = new Link();
			link.setId(resultSet.getInt(ModifiedLinksContanst.ID_DB));
			link.setTitle(resultSet.getString(ModifiedLinksContanst.TITLE_DB));
			link.setLink(resultSet.getString(ModifiedLinksContanst.LINK_DB));
//			link.setOrder(resultSet.getInt(ModifiedLinksContanst.ORDER_DB));
			link.setOrder(order);
			link.setStatus(ModifiedLinksContanst.NORMAL_LINK_STATUS);
			links.add(link);
			++order;
		}
		// close statement, result set
		resultSet.close();
		close();
		return links;
	}

	@Override
	public boolean updateLink(Link link) throws SQLException {
		// TODO Auto-generated method stub
		Object[] params = new Object[4];
		params[0] = link.getTitle();
		params[1] = link.getLink();
		params[2] = link.getOrder();
		params[3] = link.getId();
		
		return executeUpdate(SQLStatementLoader.getLinkUpdateQuery(), params);
	}

	@Override
	public boolean insertLink(Link link) throws SQLException {
		Object[] params = new Object[3];
		params[0] = link.getTitle();
		params[1] = link.getLink();
		params[2] = link.getOrder();
		
		return executeUpdate(SQLStatementLoader.getLinkInsertQuery(), params);
	}

	@Override
	public boolean deleteLink(Link link) throws SQLException {
		Object[] params = new Object[1];
		params[0] = link.getId();
		
		return executeUpdate(SQLStatementLoader.getLinkDeleteQuery(), params);
	}

}
